<?php
namespace api\test\controller;

use api\test\model\ClassModel;
use api\test\model\MajorModel;
use api\test\model\SchoolModel;
use api\test\model\StudentModel;
use cmf\controller\RestBaseController;
use think\db\Where;

Class SchoolController extends RestBaseController
{
    //获取学校列表
    public function getSchoolList () {

        //分页
        $page = input('page') ? input('page') : 1;

        //搜索
        $name = input('name');

        $where = new Where();

        if ($name) {
            $where['name'] = ['like','%'.$name.'%'];
        }

        $data = SchoolModel:: where($where)
            ->page($page,2)

            ->select();

        $this->success('查询成功',$data);
    }
    //通过学校ID查专业
    public function getMajor ()
    {
        $school_id = input('school_id') ? input('school_id') : 1;
        $where = new Where();
        if ($school_id) {
            $where['school_id'] = $school_id;
         }
        $data = MajorModel::where($where)->select();
        $this->success('查询成功',$data);
    }
    //通过专业ID查班级
    public function getClass()
    {
        $major_id = input('major_id') ? input('major_id') : 1;
        $where = new Where();
        if ($major_id) {
            $where['major_id'] = $major_id;
        }
        $data = ClassModel::where($where)->select();
        $this->success('查询成功',$data);
    }

    //添加
    public function add ()
    {
        $name =  $_POST['name'];
        $city = $_POST['city'];
        $add = SchoolModel::create([
            'name' => $name,
            'city' => $city
        ]);
        if ($add) {
            $this->success('添加成功');
        }else{
            $this->error('添加失败');
        }
    }

    //修改
    public function edit ()
    {
        $id = $_GET['id'];
        $name =  $_POST['name'];
        $city = $_POST['city'];
        $edit = SchoolModel::where('id',$id)->update(['name' => $name,'city' => $city]);
        if ($edit) {
             $this->success('修改成功');
        }else{
             $this->error('修改失败');
        }
    }
    //删除
    public function del ()
    {
        $id = $_GET['id'];
        $del = SchoolModel::destroy($id);
        if ($del) {
             $this->success('删除成功');
        }else{
             $this->error('删除失败');
        }
    }
    //批量删除
    public function delAll()
    {
        $arr = $_GET['ids'];
        $del = SchoolModel::destroy($arr);
        if ($del) {
             $this->success('删除成功');
        }else{
             $this->error('删除失败');
        }
    }

}